<template>
  <div class="xtx-goods-page" v-if="goodDetail.categories">
    <!-- v-if="goodDetail.categories" 添加:对象获取属性值,超过一层 -->
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodDetail.categories[1].id}`">{{
          goodDetail.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodDetail.categories[0].id}`">{{
          goodDetail.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 新增结构 -->
        <!-- 图片预览区=>左侧 -->
        <div class="media">
          <GoodsImage :pics="goodDetail.mainPictures"></GoodsImage>
          <!-- 商品售卖信息 -->
          <GoodsSales :goodDetail="goodDetail"></GoodsSales>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodName :goodDetail="goodDetail"></GoodName>
          <!-- 商品sku组件 -->
          <GoodsSku :goods="goodDetail" @change="getSku"></GoodsSku>
          <!-- 商品的数量加减组件
          :max="goodDetail.inventory"
          把商品最大的库存传过去
           -->
          <XtxNumbox :max="goodDetail.inventory" v-model="buyNum"></XtxNumbox>
          <XtxButton style="margin-top: 20px" @click="addCart">
            加入购物车
          </XtxButton>
        </div>
      </div>

      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs></GoodsTabs>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot v-for="hot in 3" :key="hot" :type="hot"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods.js'
import { useRoute } from 'vue-router'
import { ref, onMounted, provide } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/Message/index.js'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodName,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    // 商品详情数据
    const goodDetail = ref({})
    // 商品购买
    const route = useRoute()
    const buyNum = ref(1)
    provide('goodDetail', goodDetail)

    const getGoodDetail = async () => {
      const { result } = await findGoods(route.params.id)
      console.log('商品详情来自Good/index:', result)
      goodDetail.value = result
    }
    // 获取当前选择的sku数据
    /**
     * 有效sku包含俩个信息
     * 1.库存
     * 2.价格
     *
     *
     * */
    let skuGood = null
    const getSku = (sku) => {
      console.log('sku:', sku)
      if (sku.skuId) {
        goodDetail.value.price = sku.price
        goodDetail.value.oldprice = sku.oldprice
        goodDetail.value.inventory = sku.inventory
        skuGood = sku
      } else {
        skuGood = null
      }
    }

    // 加入购物车
    const store = useStore()
    const addCart = () => {
      /**
       * 思路:
       * 1.必须是有效的sku商品才能加入购物车
       * 2.必须有库存sku.inventory>0
       * 3.执行加入购物车逻辑=>存储到vuex
       */
      if (!skuGood) return Message({ type: 'error', text: '请选择商品规格!' })
      if (skuGood.inventory === 0) return Message({ type: 'error', text: '该商品没有库存!' })
      store.dispatch('cart/addCartAction', {
        id: goodDetail.value.id,
        name: goodDetail.value.name,
        picture: goodDetail.value.mainPictures[0],
        skuId: skuGood.skuId,
        price: skuGood.oldPrice,
        nowPrice: skuGood.price,
        attrsText: skuGood.specsText,
        stock: skuGood.inventory,
        selected: true, // 购物车商品选中状态
        isEffective: true, // 是否是有效状态
        count: buyNum.value
      }).then((msg) => {
        console.log('1', msg)
        Message({ type: 'success', text: '加入购物车成功!' })
      })
    }
    onMounted(() => {
      getGoodDetail()
    })
    return { goodDetail, getSku, addCart, buyNum }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
